package com.yzt.message.custom;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yzt.zhuju.utils.DiceUtil;
import com.yzt.plugin_imageloader.ImageLoadUtil;
import com.yzt.message.R;

import java.util.List;

import io.rong.imkit.conversation.extension.component.emoticon.AndroidEmoji;
import io.rong.imkit.model.UiMessage;
import io.rong.imkit.widget.adapter.IViewProviderListener;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.MessageContent;

/**
 * 骰子的容器
 */
public class RXDiceMessageItemProvider extends CustomBaseMessageItemProvider<RXDiceMessageContent> {

    public RXDiceMessageItemProvider() {
        mConfig.showReadState = true;
        mConfig.showContentBubble = false;
    }

    @Override
    protected ViewHolder onCreateMessageContentViewHolder(ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.nomi_rc_item_dice_message, parent, false);
        return new ViewHolder(view.getContext(), view);
    }

    @Override
    protected void bindMessageContentViewHolder(io.rong.imkit.widget.adapter.ViewHolder viewHolder,
                                                io.rong.imkit.widget.adapter.ViewHolder viewHolder1, RXDiceMessageContent content,
                                                UiMessage data, int position, List<UiMessage> list,
                                                IViewProviderListener<UiMessage> iViewProviderListener) {
        ViewHolder holder = (ViewHolder) viewHolder;

        if (data.getMessageDirection() == Message.MessageDirection.SEND) {
            holder.llMainLeft.setVisibility(View.GONE);
            holder.llMain.setVisibility(View.VISIBLE);
        } else {
            holder.llMainLeft.setVisibility(View.VISIBLE);
            holder.llMain.setVisibility(View.GONE);
        }

        ImageLoadUtil.loadIv(holder.ivPhoto.getContext(), DiceUtil.getDrawableId(Integer.parseInt(content.getContent())), holder.ivPhoto);
        ImageLoadUtil.loadIv(holder.ivPhotoLeft.getContext(), DiceUtil.getDrawableId(Integer.parseInt(content.getContent())), holder.ivPhotoLeft);
    }

    @Override
    protected boolean onItemClick(io.rong.imkit.widget.adapter.ViewHolder viewHolder, RXDiceMessageContent content,
                                  UiMessage message, int position, List<UiMessage> list,
                                  IViewProviderListener<UiMessage> iViewProviderListener) {
        return false;
    }

    @Override
    protected boolean isMessageViewType(MessageContent messageContent) {
        return messageContent instanceof RXDiceMessageContent && !messageContent.isDestruct();
    }

    @Override
    public Spannable getSummarySpannable(Context context, RXDiceMessageContent data) {
        if (data == null) {
            return null;
        }
        String content = data.getContent();
        if (content != null) {
            if (content.length() > 100) {
                content = content.substring(0, 100);
            }
            return new SpannableString(AndroidEmoji.ensure(content));
        }
        return null;
    }

    private static class ViewHolder extends io.rong.imkit.widget.adapter.ViewHolder {
        LinearLayout llMain, llMainLeft;
        ImageView ivPhoto, ivPhotoLeft;

        public ViewHolder(Context context, View view) {
            super(context, view);

            this.llMain = view.findViewById(R.id.ll_main);
            this.llMainLeft = view.findViewById(R.id.ll_main_left);
            this.ivPhoto = view.findViewById(R.id.iv_photo);
            this.ivPhotoLeft = view.findViewById(R.id.iv_photo_left);
        }
    }
}